package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.PmsPortraitDto;
import com.ruoyi.system.domain.po.PmsPortrait;
import com.ruoyi.system.domain.po.PmsPortraitExample;
import com.ruoyi.system.domain.vo.PmsPortraitVo;
import com.ruoyi.system.mapper.PmsPortraitMapper;
import com.ruoyi.system.service.PmsPortraitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author nanyang
 */
@Service
public class PmsPortraitServiceImpl implements PmsPortraitService {
    
    @Resource
    private PmsPortraitMapper pmsPortraitMapper;
    
    
    @Override
    public Integer insertPmsPortrait(PmsPortraitDto.Insert pmsPortraitInsert) {
        // 新增实体数据
        PmsPortrait pmsPortrait = BeanUtil.copyProperties(pmsPortraitInsert, PmsPortrait.class);
        pmsPortrait.setId(IdUtil.getSnowflake().nextId());
        pmsPortrait.setCreateBy(SecurityUtils.getUsername());
        pmsPortrait.setCreateTime(new Date());
        return pmsPortraitMapper.insertSelective(pmsPortrait);
    }

    @Override
    public Integer updatePmsPortrait(PmsPortraitDto.Update pmsPortraitUpdate) {
        // 修改实体
        PmsPortrait pmsPortrait = BeanUtil.copyProperties(pmsPortraitUpdate, PmsPortrait.class);
        pmsPortrait.setUpdateBy(SecurityUtils.getUsername());
        pmsPortrait.setUpdateTime(new Date());
        return pmsPortraitMapper.updateByPrimaryKeySelective(pmsPortrait);
    }

    @Override
    public Integer deletePmsPortrait(PmsPortraitDto.Delete pmsPortraitDelete) {
        PmsPortraitExample interfaceExample = new PmsPortraitExample();
        interfaceExample.createCriteria().andIdIn(pmsPortraitDelete.getIdList());
        PmsPortrait pmsPortrait = new PmsPortrait();
        pmsPortrait.setDelFlag((byte) 2);
        return pmsPortraitMapper.updateByExampleSelective(pmsPortrait, interfaceExample);
    }

    @Override
    public List<PmsPortraitVo.Result> listPmsPortrait(PmsPortraitDto.Query queryPmsPortrait) {
        PmsPortraitExample pmsPortraitExample = new PmsPortraitExample();
        PmsPortraitExample.Criteria criteria = pmsPortraitExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (queryPmsPortrait.getGender() != null) {
            criteria.andGenderEqualTo(queryPmsPortrait.getGender());
        }
        if (queryPmsPortrait.getQualification() != null) {
            criteria.andQualificationEqualTo(queryPmsPortrait.getQualification());
        }
        if (queryPmsPortrait.getJob() != null) {
            criteria.andJobEqualTo(queryPmsPortrait.getJob());
        }
        if (queryPmsPortrait.getAge() != null) {
            criteria.andMaxAgeGreaterThanOrEqualTo(queryPmsPortrait.getAge()).andMinAgeLessThanOrEqualTo(queryPmsPortrait.getAge());
        }
        pmsPortraitExample.setOrderByClause("create_time desc");
        List<PmsPortrait> pmsPortraitList = pmsPortraitMapper.selectByExample(pmsPortraitExample);
        return pmsPortraitList.stream().map(e -> BeanUtil.copyProperties(e, PmsPortraitVo.Result.class)).collect(Collectors.toList());
    }

}
